/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.viettel.bkno.visionone.services;

import com.viettel.bkno.visionone.business.DeviceManager;
import com.viettel.bkno.visionone.business.DisplayCalendarManager;
import com.viettel.bkno.visionone.business.ResourceManager;
import com.viettel.bkno.visionone.business.SessionManager;
import com.viettel.bkno.visionone.business.UserManager;
import com.viettel.bkno.visionone.business.interfaces.IDeviceManager;
import com.viettel.bkno.visionone.business.interfaces.IDisplayCalendarManager;
import com.viettel.bkno.visionone.business.interfaces.IResourceManager;
import com.viettel.bkno.visionone.business.interfaces.ISessionManager;
import com.viettel.bkno.visionone.business.interfaces.IUserManager;
import com.viettel.bkno.visionone.exception.CoreServiceException;
import com.viettel.bkno.visionone.untils.ConfigType;
import com.viettel.bkno.visionone.untils.ConfigurationManager;
import com.viettel.vinamilk.memcached.MemcachedClientManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author hienpt9
 */
public class ServicesManager {

    private static ServicesManager instance = new ServicesManager();
    boolean running = false;

    IDeviceManager deviceManager;
    IUserManager userManager;
    IResourceManager resoureManager;
    ISessionManager sessionManager;
    IDisplayCalendarManager displayCalendarManager;

    Logger logger = Logger.getLogger(ServicesManager.class);

    private ServicesManager() {
        ConfigurationManager.getInstance().load();
    }

    public static ServicesManager getInstance() {
        if (instance == null) {
            instance = new ServicesManager();
        }
        return instance;
    }

    /**
     * Khoi dong Service Manager
     */
    synchronized public void start() {
        if (running) {
            return;
        }
        configLog4j();
        startMemcachedClient();
        deviceManager = new DeviceManager();
        userManager = new UserManager();
        resoureManager = new ResourceManager();
        sessionManager = new SessionManager();
        displayCalendarManager = new DisplayCalendarManager();
        logger.warn("[OK] ServicesManager started.");
        running = true;
    }

    /**
     * cau hinh Log4j
     */
    public void configLog4j() {
        try {
            String log4jConfigFilePath = ConfigurationManager.getInstance().getConfigValue(ConfigType.LOG4J_CONFIG_FILE_PATH);
            PropertyConfigurator.configure(log4jConfigFilePath);
        } catch (CoreServiceException ex) {
            logger.warn("Log4j configure unsuccessful. Default configuration will be used.", ex);
        }
    }

    /**
     * Khoi dong memcached server client
     */
    public void startMemcachedClient() {
        String memcachedServers;
        try {
            // Doc thong tin server memcached
            memcachedServers = ConfigurationManager.getInstance().getConfigValue(ConfigType.MEMCACHED_SERVERS);
            // Khoi dong memcached server client
            MemcachedClientManager.start(memcachedServers);
        } catch (Exception ex) {
            logger.warn("Khoi dong memcached server client co loi."
                    + "\n Du lieu se khong the cached.", ex);
        }
    }

    public IDeviceManager getDeviceManager() {
        return deviceManager;
    }

    public void setDeviceManager(IDeviceManager deviceManager) {
        this.deviceManager = deviceManager;
    }

    public IUserManager getUserManager() {
        return userManager;
    }

    public void setUserManager(IUserManager userManager) {
        this.userManager = userManager;
    }

    public IResourceManager getResoureManager() {
        return resoureManager;
    }

    public void setResoureManager(IResourceManager resoureManager) {
        this.resoureManager = resoureManager;
    }

    public ISessionManager getSessionManager() {
        return sessionManager;
    }

    public void setSessionManager(ISessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    public IDisplayCalendarManager getDisplayCalendarManager() {
        return displayCalendarManager;
    }

    public void setDisplayCalendarManager(IDisplayCalendarManager displayCalendarManager) {
        this.displayCalendarManager = displayCalendarManager;
    }
}
